// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.properties.controllers;

import org.dengues.core.components.NodePropertyParameter;
import org.dengues.designer.ui.properties.PropertyTabDynamicSection;
import org.eclipse.jface.fieldassist.ControlDecoration;
import org.eclipse.jface.fieldassist.FieldDecoration;
import org.eclipse.jface.fieldassist.FieldDecorationRegistry;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Text;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf Qiang.Zhang.Adolf@gmail.com 2008-1-17 qiang.zhang $
 * 
 */
public class TextPropertyController extends AbstractPropertyController {

    /**
     * Qiang.Zhang.Adolf@gmail.com TextPropertyController constructor comment.
     * 
     * @param dps
     */
    public TextPropertyController(PropertyTabDynamicSection dps) {
        super(dps);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.designer.ui.properties.controllers.AbstractPropertyController#createControl(org.eclipse.swt.widgets.Composite,
     * org.dengues.core.components.NodePropertyParameter, org.eclipse.swt.widgets.Control)
     */
    @Override
    public Control createControl(Composite composite, NodePropertyParameter param, Control lastControl) {
        Composite subcomposite = (Composite) super.createControl(composite, param, lastControl);
        Text labelText;
        CLabel cLabel = getWidgetFactory().createCLabel(subcomposite, param.getDisplayName());
        GridData gridData = new GridData();
        gridData.widthHint = STANDARD_LABEL;
        cLabel.setLayoutData(gridData);
        int nbLines = param.getNbLines();
        Text text = null;
        if (nbLines > 0) {
            text = getWidgetFactory().createText(subcomposite, "", SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL); //$NON-NLS-1$
        } else {
            text = getWidgetFactory().createText(subcomposite, "", SWT.BORDER); //$NON-NLS-1$
        }
        ControlDecoration controlDecoration = new ControlDecoration(text, SWT.RIGHT | SWT.TOP);
        if (param.isRequired()) {
            FieldDecoration fieldDecoration = FieldDecorationRegistry.getDefault().getFieldDecoration(
                    FieldDecorationRegistry.DEC_REQUIRED);
            controlDecoration.setImage(fieldDecoration.getImage());
            controlDecoration.setDescriptionText(fieldDecoration.getDescription());
        }
        labelText = (Text) controlDecoration.getControl();
        register(param, labelText);
        if (param.getValue() != null) {
            labelText.setText(param.getValue().toString());
        }
        GridData data = new GridData(GridData.FILL_HORIZONTAL);
        if (nbLines != 0) {
            data.heightHint = text.getLineHeight() * nbLines;
        } else {
            data.heightHint = SWT.DEFAULT;
        }

        data.horizontalIndent = FieldDecorationRegistry.getDefault().getMaximumDecorationWidth();
        labelText.setLayoutData(data);
        labelText.setData(PARAMETER_NAME, param.getName());
        labelText.setEditable(!param.isReadOnly());
        labelText.setBackground(param.isReadOnly() ? Display.getDefault().getSystemColor(SWT.COLOR_GRAY) : subcomposite
                .getBackground());
        curControlMap.put(param.getName(), labelText);
        return labelText;
    }

}
